
package com.sm.shoplook.controller.aboutus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.about.AboutUs;
import com.sm.shoplook.service.about.AboutUsService;
import com.sm.shoplook.util.FileUtil;
import com.sm.shoplook.vo.about.AboutUsSearchReqVo;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;

/**
 * @author chenpy
 * @desc 关于我们
 * @date 2018/8/16 16:31
 * @see
 */
@Controller
@RequestMapping("/about")
public class AboutUsController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AboutUsController.class);
	@Autowired
	private AboutUsService aboutUsService;
	@Autowired
	private FileUtil fileUtil;

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String aboutUsManger() {
		return "/about/about_manager";
	}

	/**
	 * @param aboutUsSearchReqVo
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 关于我们
	 */
	@RequestMapping(value = "/selectAboutUsPage", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<Map<String, Object>> selectAboutUsPage(AboutUsSearchReqVo aboutUsSearchReqVo) {
		Map<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<AboutUs>> pageInfoApiResult = aboutUsService.selectAboutUsPage(aboutUsSearchReqVo);
		PageInfo<AboutUs> pageInfo = pageInfoApiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	/**
	 * @param id
	 * @param model
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 跳转素材保存页面
	 */
	@RequestMapping(value = "/save_about_page/{id}", method = RequestMethod.GET)
	public String addOrUpdateAboutPage(@PathVariable(value = "id") Integer id, Model model) {
		LOGGER.info("id: {}", id);
		ApiResult<AboutUs> aboutUsApiResult = aboutUsService.selectAboutUsById(id);
		AboutUs aboutUs = aboutUsApiResult.getData();
		if (aboutUs == null) {
			aboutUs = new AboutUs();
		}
		model.addAttribute("aboutUs", aboutUs);
		return "/about/add_update_about";
	}

	/**
	 * @param ids
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 删除关于我们
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteAboutUs", method = RequestMethod.POST)
	public ApiResult deleteAboutUs(String ids) {
		String[] idList = ids.split(",");
		// 批量删除
		LOGGER.info("ids", ids);
		try {
			return aboutUsService.deleteAboutUs(idList);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @param aboutUs
	 * @param imgUrlFile
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 保存
	 */
	@ResponseBody
	@RequestMapping(value = "/saveAboutUs", method = RequestMethod.POST)
	public ApiResult saveRole(AboutUs aboutUs, MultipartFile imgUrlFile) {
		LOGGER.info("aboutUs", aboutUs);
		try {
			return aboutUsService.saveAboutUs(aboutUs, imgUrlFile);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 所有的关于我们
	 */
	@RequestMapping(value = "/selectAllAboutUs", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<List<AboutUs>> selectAllAboutUs() {
		return aboutUsService.selectAllAboutUs();
	}
}
